// pages/data/index.js
var wxCharts = require('../../utils/charts.js');
import initCalendar from '../../template/calendar/index';
import { switchView } from '../../template/calendar/index';
import { jump } from '../../template/calendar/index';
var app = getApp();
// var wxCharts = app.wxCharts;
var lineChart = null;
const series = [{
  name: '高压(收缩压)mmHg',
  data: [130, 120, 102, 110, 134, 129, 120, 112, 133],
  // format: function (val, name) {
  //   return val.toFixed(2) + '万';
  // },
  color: '#e92200',

}, {
  name: '低压(舒张压)mmHg',
  data: [73, 78, 82, 81, 82, 88, 82, 78, 80],
  // format: function (val, name) {
  //   return val.toFixed(2) + '万';
  // },
  color: "#50b025",
}, {
  name: '脉搏(次/分)',
  data: [70, 72, 76, 90, 78, 80, 73, 72, 71],
  // format: function (val, name) {
  //   return val.toFixed(2) + '万';
  // },
  color: "#f5d500",
}];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    series,
    alertStatus: false,
    calendarIcon: {
      left: '<',
      right: '>'
    },
    calendarDate: new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-'+new Date().getDate(),
  },
  touchHandler: function (e) {
    // console.log(lineChart.getCurrentDataIndex(e));
    lineChart.showToolTip(e, {
      // background: '#7cb5ec',
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  createSimulationData: function () {
    var categories = [];
    var data = [];
    for (var i = 0; i < 10; i++) {
      categories.push('04/23');
      data.push(Math.random() * (20 - 10) + 10);
    }
    // data[4] = null;
    return {
      categories: categories,
      data: data
    }
  },
  updateData: function () {
    var simulationData = this.createSimulationData();
    // var series = [{
    //   name: '成交量1',
    //   data: simulationData.data,
    //   format: function (val, name) {
    //     return val.toFixed(2) + '万';
    //   }
    // }];
    lineChart.updateData({
      categories: simulationData.categories,
      series: series
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this;
    initCalendar({
      multi: true, // 是否开启多选,
      disablePastDay: false, // 是否禁选过去日期
      showInput: false, // 默认为 true
      placeholder: '请选择日期', // input 输入框
      type: 'normal',
      /**
       * 选择日期后执行的事件
       * @param { object } currentSelect 当前点击的日期
       * @param { array } allSelectedDays 选择的所有日期（当mulit为true时，才有allSelectedDays参数）
       */
      afterTapDay: (currentSelect, allSelectedDays) => {
        console.log(currentSelect, '---')
      },
      /**
       * 日期点击事件（此事件会完全接管点击事件）
       * @param { object } currentSelect 当前点击的日期
       * @param { object } event 日期点击事件对象
       */
      onTapDay(currentSelect, event) {
        console.log(currentSelect, '---2')
        jump(currentSelect.year, currentSelect.month, currentSelect.day);
        that.setData({
          alertStatus:false,
          calendarDate: currentSelect.year + '-' + currentSelect.month + '-' + currentSelect.day
        })

      },
      /**
       * 日历初次渲染完成后触发事件，如设置事件标记
       */
      afterCalendarRender() { },
    });

    this.drawCanvas();
  },
  //  画图
  drawCanvas(){
    var windowWidth = 375, winHei = 620;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
      winHei = 274 * windowWidth / 375;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    var simulationData = this.createSimulationData();
    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: ['04/23', '04/23', '04/23', '04/23', '04/23', '04/23', '04/23', '04/23', '04/23'],
      animation: true,
      background: '#fff',
      series,
      xAxis: {
        min: 'auto',
        max: 'auto',
        disableGrid: true,
        gridColor: '#e2e2e2',
        fontColor: "#4a4a4a",
      },
      yAxis: {
        disabled: false,
        gridColor: '#e2e2e2',
        fontColor: "#4a4a4a",
        format: function (val) {
          return val.toFixed(2);
        },
        min: 'auto',
        max: 'auto'
      },
      width: windowWidth,
      height: winHei,
      dataLabel: true,
      dataPointShape: true,
      extra: {
        show: false,
        lineStyle: 'straight',//straight curve
        legendTextColor: "#00ff00",
        labelColor: "#f00"
      },
      legend: false,
      dataItem: {
        color: "#00ff00"
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  calendarShow(){
    this.setData({
      alertStatus:true
    })
  },
  calendarToggle(){
    var alertStatus = this.data.alertStatus;
    this.setData({
      alertStatus: !alertStatus
    })
  },
  calendarHide() {
    this.setData({
      alertStatus: false
    })
  },
  
})